﻿//var req;
//var resultPanelID = "";
var strLoadedModule = "";
var expandSnapTimer = null;

Function.prototype.andThen=function(g) {
  var f=this;
  return function() {
    f();g();
  }
};

function CallBackManager() {
  this.callback=function () {}; // do nothing
  this.registerCallback=function(callbackFunction) {
    this.callback=(this.callback).andThen(callbackFunction);
  }
}

function Initialize()
{
	var _req;
	try
	{
		_req=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			_req=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			_req=null;
		}
	}

	if(!_req&&typeof XMLHttpRequest!="undefined")
	{
		_req=new XMLHttpRequest();
	}
	
	return _req;
	
}

function SendQuery(tr, mr, nr)
{	
	if (expandSnapTimer != null) window.clearTimeout(expandSnapTimer);
	expandSnapTimer = null;
	this.tr = tr;
	this.mr = mr;
	this.nr = nr;
	var me = this;
	this.callback=function () {}; // do nothing
	this.registerCallback=function(callbackFunction) {
		this.callback=(this.callback).andThen(callbackFunction);
	}
	
	this.invoke = function () {
		this.resultPanelID = "MB-" + me.mr;
		if (strLoadedModule.indexOf('#' + this.resultPanelID + '#') >= 0) return;
			
		this.req = Initialize();
		var urlPrefix = window.location.href;
		urlPrefix = urlPrefix.substr(0, urlPrefix.lastIndexOf("/") + 1);
		var url= urlPrefix + "ProcessModule.aspx";
		
		var sqReg = me;
		var invokeReq = this.req;
		var invokeCls = this;
		
		try
		{	
			if(invokeReq!=null)
			{	
				invokeReq.onreadystatechange = 
										function ()
										{
											if (invokeReq.readyState == 4) 
												{
												// only if "OK"
													if (invokeReq.status == 200) 
													{
														if(invokeReq.responseText!="")
														{
															try
															{
																var _intStartTrace = invokeReq.responseText.indexOf('<StartModuleTrace>') + 18;
																var _intEndTrace = invokeReq.responseText.indexOf('</StartModuleTrace>');
																var _strModuleContent = invokeReq.responseText.substring(_intStartTrace, _intEndTrace);
																_strModuleContent = replacestring(_strModuleContent, '<Script', '<script', false);
																
																var _intScriptTrace  = _strModuleContent.indexOf('<script');
																var _intEndScriptTrace = _strModuleContent.indexOf('</script>', _intScriptTrace);
																var _intSrcPos = _strModuleContent.indexOf('src="', _intScriptTrace);
																var _intLinkTrace  = _strModuleContent.indexOf('<link');
																
																var _strScripts = '';
																var _strCheckScriptTag = '';
																var _strCheckLinkTag = '';						
																//alert(_strModuleContent);
																
																if (_intLinkTrace >= 0)
																{
																	_strCheckLinkTag = _strModuleContent.substring(_intLinkTrace, _strModuleContent.indexOf('>', _intLinkTrace))
																	if (_strCheckLinkTag.length > 0)
																	{
																		var _intStartHref = _strCheckLinkTag.indexOf('href="') + 6;
																		var _intEndHref = _strCheckLinkTag.indexOf('"', _intStartHref);
																		_strCheckLinkTag = _strCheckLinkTag.substring(_intStartHref, _intEndHref);								
																		loadLink(_strCheckLinkTag);								
																	}
																}
																//alert(_strCheckLinkTag);
																while (_intScriptTrace >= 0 && _intEndScriptTrace > _intScriptTrace && ((_intSrcPos > _intScriptTrace)&&(_intSrcPos < _intEndScriptTrace)))
																{							
																	_strCheckScriptTag = _strModuleContent.substring(_intScriptTrace, _strModuleContent.indexOf('>', _intScriptTrace))
																	if (_strCheckScriptTag.indexOf('src') > 0)
																	{
																		var _intStartSrc = _strCheckScriptTag.indexOf('src="') + 5;
																		var _intEndSrc = _strCheckScriptTag.indexOf('"', _intStartSrc);
																		_strCheckScriptTag = _strCheckScriptTag.substring(_intStartSrc, _intEndSrc);
																		if (_strCheckScriptTag.length > 0)
																		{														
																			loadScript(_strCheckScriptTag);									
																			_strModuleContent = _strModuleContent.substring(0, ((_intScriptTrace == 0) ? _intScriptTrace : (_intScriptTrace))) + _strModuleContent.substr(_intEndScriptTrace + 9);
																			//alert(_strModuleContent);									
																		}
																	}						
																	
																	_intScriptTrace  = _strModuleContent.indexOf('<script');
																	_intSrcPos = _strModuleContent.indexOf('src="', _intScriptTrace);
																	_intEndScriptTrace = _strModuleContent.indexOf('</script>', _intScriptTrace);
																	
																	/*alert("_intScriptTrace = " + _intScriptTrace);
																	alert("_intSrcPos = " + _intSrcPos);
																	alert("_intEndScriptTrace = " + _intEndScriptTrace);*/
																}						
																if (invokeCls.resultPanelID == "MB-521ac225-8800-418d-a9cf-c76093b9a7d2" || invokeCls.resultPanelID == "MB-8e521128-ad52-47ee-b703-95d31460e2c8")
																{
																	alert(_strModuleContent);
																	alert("innerHTML = " + document.getElementById(invokeCls.resultPanelID).innerHTML);
																}
																document.getElementById(invokeCls.resultPanelID).innerHTML = _strModuleContent;
																_intScriptTrace  = _strModuleContent.indexOf('<script');
																_intEndScriptTrace = _strModuleContent.indexOf('</script>', _intScriptTrace);
																while (_intScriptTrace >= 0 && _intEndScriptTrace > _intScriptTrace)
																{
																	/*alert("_intScriptTrace = " + _intScriptTrace);
																	alert("_intEndScriptTrace = " + _intEndScriptTrace);	
																	alert("_strModuleContent.indexOf('>', _intScriptTrace) + 1 = " + (_strModuleContent.indexOf('>', _intScriptTrace) + 1));*/
																	if (_strModuleContent.indexOf('>', _intScriptTrace) + 1 < _intEndScriptTrace)
																	{
																		_strScripts = _strModuleContent.substring(_strModuleContent.indexOf('>', _intScriptTrace) + 1, _intEndScriptTrace - 1);
																		//alert(_strScripts);
																		try
																		{
																			if (_strScripts.length > 0) eval(_strScripts);
																		}
																		catch(EvalScriptError)
																		{
																			alert('Error : ' + EvalScriptError.description + '\nScripts : \n' + _strScripts);
																		}
																	}
																	
																	_strModuleContent = _strModuleContent.substring(0, ((_intScriptTrace == 0) ? _intScriptTrace : (_intScriptTrace - 1))) + _strModuleContent.substr(_intEndScriptTrace + 9);
																	_intScriptTrace  = _strModuleContent.indexOf('<script');
																	_intEndScriptTrace = _strModuleContent.indexOf('</script>', _intScriptTrace);
																}
																strLoadedModule += '#' + invokeCls.resultPanelID + '#';
																invokeReq.abort();
																
															}
															catch(er)
															{
																alert('Outside Error : ' + er.message + '\n');
															}
															
															sqReg.callback();
														}
													}
													else 
													{
														document.getElementById(invokeCls.resultPanelID).innerHTML="<font style=\"font : Verdana 11px;\">Lỗi xảy ra : " + invokeReq.statusText + "</font>";
													}
												}
										};
				//req.open("GET", url, false);
				invokeReq.open("POST", url, false);
				if (me.tr != null && me.tr != "") invokeReq.setRequestHeader("TR", encodeURIComponent(me.tr));
				if (me.mr != null && me.mr != "") invokeReq.setRequestHeader("MR", encodeURIComponent(me.mr));
				if (me.nr != null)
				{
					me.nr = me.nr == "" ? "0" : me.nr;
					invokeReq.setRequestHeader("NR", encodeURIComponent(me.nr));
				}
				invokeReq.send(null);
			}
		}
		catch(e)
		{
			alert('Root Error : ' + e.message);
		}
	}
	
}

function replacestring(str_normal,str_find,str_replace,int_case_insensitive)
{
	if (arguments.length<3 || str_find=="" || str_normal=="" || typeof("".split)!="function")
		return(str_normal);

	//no parm means default, "case SENSITIVE"...
	if(!(int_case_insensitive))
		return(str_normal.split(str_find)).join(str_replace);

	str_find=str_find.toLowerCase();

	var rv="";
	var ix=str_normal.toLowerCase().indexOf(str_find);
	while(ix>-1)
	{
		rv+=str_normal.substring(0,ix)+str_replace;
		str_normal=str_normal.substring(ix+str_find.length);
		ix=str_normal.toLowerCase().indexOf(str_find);
	};
	return(rv+str_normal);
};//end function replacestring





function loadScript (url) {
  if (document.layers)
    window.location.href = url;
  else if (document.getElementById) {
    var script = document.createElement('script');
    script.defer = true;
    script.src = url;
    script.type = "text/javascript";
    document.getElementsByTagName('head')[0].appendChild(script);    
  }
  
  /*document.open();
  document.write('<script src="', url, '" type="text/JavaScript"><\/script>');  
  document.close();*/
}

function loadLink (url) {
  if (document.layers)
    window.location.href = url;
  else if (document.getElementById) {
    var link = document.createElement('link');    
    link.href = url;
    link.rel = "stylesheet";
    link.type = "text/css";
    document.getElementsByTagName('head')[0].appendChild(link);
  }
}

onerror=handleErr;
var txt="";

function handleErr(msg,url,l)
{
txt="There was an error on this page.\n\n";
txt+="Error: " + msg + "\n";
txt+="URL: " + url + "\n";
txt+="Line: " + l + "\n\n";
txt+="Click OK to continue.\n\n";
alert(txt);
return true;
}